package com.jamy.song.demo.controller;


import com.alibaba.fastjson.JSON;
import com.jamy.song.demo.config.EsClientPool;
import com.jamy.song.demo.config.EsConfig;
import com.jamy.song.demo.entity.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

@Controller
@Slf4j
public class TestEsPoolController {

    @Resource
    private EsClientPool esClientPool;

    @RequestMapping("/testPool/{id}")
    @ResponseBody
    public String testEsPool(@PathVariable("id") String id) {
        /*SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        SearchRequest searchRequest = new SearchRequest(EsConfig.ES_INDEX);
        searchRequest.source(searchSourceBuilder);*/
        RestHighLevelClient client = null;
        UserInfo userInfo = new UserInfo();
        try {
            client = esClientPool.getClient();
            // 获取请求对象
            GetRequest getRequest = new GetRequest("mydlq-user", "doc", id);
            // 获取文档信息
            GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
            if (response.isExists()) {
                userInfo = JSON.parseObject(response.getSourceAsBytes(), UserInfo.class);
                log.info("员工信息：{}", userInfo);
            }
        } catch (Exception e) {
            log.info("ES search fail", e);
        } finally {
            if (client != null) {
                esClientPool.returnClient(client);
            }
        }
        return userInfo.toString();
    }


    @RequestMapping("/addData")
    @ResponseBody
    public String adddata() {
        RestHighLevelClient client = null;
        try {
            // 创建索引请求对象
            IndexRequest indexRequest = new IndexRequest("mydlq-user", "doc", "2");
            // 创建员工信息
            UserInfo userInfo = new UserInfo();
            userInfo.setName("里斯");
            userInfo.setAge(30);
            userInfo.setAddress("上海市");
            userInfo.setRemark("来自上海市的里先生");
            userInfo.setCreateTime(new Date());
            userInfo.setBirthDate("1993-01-10");
            // 将对象转换为 byte 数组
            byte[] json = JSON.toJSONBytes(userInfo);
            // 设置文档内容
            indexRequest.source(json, XContentType.JSON);
            // 执行增加文档
            client = esClientPool.getClient();
            log.info("es_client:" + client);
            IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
            log.info("创建状态：{}", response.status());
        } catch (Exception e) {
            log.info("ES search fail", e);
            return "添加数据错误";
        } finally {
            if (client != null) {
                esClientPool.returnClient(client);
            }
        }
        return "add data success!";
    }
}
